﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.Base;
using Ext.Net;
using MemberMgr.Data.ManagerObjects;
using System.Data;

namespace MemberMgr.Website.Product
{
    public partial class AddProductIn : Common.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {

            }
        }

        [DirectMethod]
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtProductID.Text))
            {
                X.Msg.Show(new MessageBoxConfig()
                {
                    Message="请先选择商品后入库。",
                    Buttons=MessageBox.Button.OK,
                    Icon=MessageBox.Icon.ERROR
                });
                return;
            }

            ItProductInlogManager productinMgr = Common.CommonUtil.managerFactory.GettProductInlogManager();
            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();

            tProduct product = productMgr.GetById(uint.Parse(this.txtProductID.Text));

            tProductInlog productinLog = new tProductInlog();

            productMgr.Session.BeginTransaction();

            try
            {
                productinLog.Inmember = base.UserInfo.Id;
                productinLog.Insum = decimal.Parse(this.txtSum.Text.Trim());
                productinLog.Intime = DateTime.Now;
                productinLog.TProduct = product;

                productinMgr.Save(productinLog);

                productMgr.Session.CommitTransaction();

                X.Msg.Show(new MessageBoxConfig()
                {
                    Message = "商品入库成功",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO
                });


                this.txtProductSum.Text = (decimal.Parse(this.txtProductSum.Text) + decimal.Parse(this.txtSum.Text)).ToString();
                this.txtSum.Text = "0";
            }
            catch (Exception ex)
            {
                productMgr.Session.RollbackTransaction();
                Common.CommonUtil.logger.Error(ex.Message, ex);
                X.Msg.Show(new MessageBoxConfig()
                {
                    Message = "商品入库失败，请重试!",
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                });
            }
        }

        [DirectMethod]
        protected void productSelect(object sender, DirectEventArgs e)
        {
            string strid = e.ExtraParams["productid"].ToString();

            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();

            tProduct product = productMgr.GetById(uint.Parse(strid));

            if (product != null)
            {

                decimal productSUM = 0;

                productSUM += product.TProductInlogs.Sum<tProductInlog>(p => p.Insum);
                productSUM = productSUM - product.TProductOutlogs.Sum<tProductOutlog>(p => p.Outsum);

                this.fpProductInfo.SetValues(new { 
                    product.Productname,
                    product.Producttype,
                    product.Maker,
                    product.Price,
                    Sum = productSUM
                });

                this.fpProductIn.SetValues(new {
                    Productid = product.Id
                });

                this.winDetail.Hide();

            }
        }

        [DirectMethod]
        protected void SearchClick(object sender, DirectEventArgs e)
        {
            this.BindDataSource();
        }

        [DirectMethod]
        protected void storeProduct_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.BindDataSource();
        }

        private void BindDataSource()
        {
            string productName = this.txtSearchName.Text;

            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();
            IList<tProduct> list = null;

            if (!string.IsNullOrEmpty(productName))
            {
                list = productMgr.GetProductsByName(productName);
            }
            else
            {
                list = productMgr.GetAll();
            }

            if (list != null)
            {
                DataTable dt = this.GetMemberInfoDataTable();

                foreach (tProduct item in list)
                {
                    dt.Rows.Add(
                        item.Id,
                        item.Productname,
                        item.Producttype,
                        item.Maker
                        );
                }

                this.storeProduct.DataSource = dt;
                this.storeProduct.DataBind();
            }
        }

        private DataTable GetMemberInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(uint)},
                new DataColumn() { ColumnName="PRODUCTNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRODUCTTYPE",DataType=typeof(string)},
                new DataColumn() { ColumnName="MAKER",DataType=typeof(string)}
              
            });

            return dt;
        }
    }
}
